package com.sicent.app0013.operate.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sicent.app0013.operate.data.AppData;
import com.sicent.app0013.operate.model.AppBarDetail;
import com.sicent.app0013.operate.model.AppVersion;
import com.sicent.app0013.operate.model.BarInfo;
import com.sicent.app0013.operate.model.InternetBar;
import com.sicent.app0013.operate.model.PushMsg;
import com.sicent.operate.common.base.dao.PageInfo;
import com.sicent.operate.common.result.ApiResult;
import com.sicent.operate.common.util.DESUtils;
import com.sicent.operate.common.util.FormatUtils;
import com.sicent.operate.common.util.QREncoder;

@RestController
@RequestMapping("app")
public class AppController {

	Logger logger = LoggerFactory.getLogger(AppController.class);
	
	@Autowired
	private AppData appData;
	
	/*
	 * 查询所有的appVersion
	 */
	@RequestMapping("/getAllAppVersion")
	public ApiResult getAllAppVersion(HttpServletRequest request) {
		List<AppVersion> list = appData.getAllAppVersion();
		return new ApiResult(list);
	}
	
	/*
	 * 添加appVersion
	 */
	@RequestMapping("/saveAppVersion")
	public ApiResult saveAppVersion(HttpServletRequest request) {
		String versionName = request.getParameter("versionName");
		int versionCode= Integer.parseInt(request.getParameter("versionCode"));
		Date planUpdateTime = FormatUtils.getDate(request.getParameter("planUpdateTime"), "yyyy-MM-dd HH:mm:ss");
		Byte status = Byte.parseByte(request.getParameter("status"));
		int  forceUpdate = Integer.parseInt(request.getParameter("forceUpdate"));
		String apkRelativePath = request.getParameter("apkRelativePath");
		String content = request.getParameter("content");
		Long apkSize = Long.parseLong(request.getParameter("apkSize"));
		int version = Integer.parseInt(request.getParameter("version"));
		int createUserId = Integer.parseInt(request.getParameter("createUserId"));
		
		logger.info("<<<<<<<<<<<<<< save app version, versionName=" + versionName + ", versionCode=" + versionCode + ", planUpdateTime=" + planUpdateTime + 
				 ", status=" + status + ", forceUpdate=" + forceUpdate + ", apkRelativePath=" + apkRelativePath + ", apkSize=" + apkSize + ",version=" + version +
				 ", createUserId=" + createUserId + ", content=" + content);
		
		AppVersion appVersion = new AppVersion();
		appVersion.setVersionName(versionName);
		appVersion.setVersionCode(versionCode);
		appVersion.setPlanUpdateTime(planUpdateTime);
		appVersion.setStatus(status);
		appVersion.setForceUpdate(forceUpdate);
		appVersion.setApkRelativePath(apkRelativePath);
		appVersion.setContent(content);
		appVersion.setApkSize(apkSize);
		appVersion.setVersion(version);
		appVersion.setCreateTime(Integer.parseInt(System.currentTimeMillis()/1000 + ""));
		appVersion.setCreateUserId(createUserId);
		
		appData.saveAppVersion(appVersion);
		return new ApiResult();
	}
	
	/*
	 * 修改appVersion
	 */
	@RequestMapping("/modifyAppVersion")
	public ApiResult modifyAppVersion(HttpServletRequest request) {
		int appVersionId = Integer.parseInt(request.getParameter("appVersionId"));
		String versionName = request.getParameter("versionName");
		int versionCode= Integer.parseInt(request.getParameter("versionCode"));
		Date planUpdateTime = FormatUtils.getDate(request.getParameter("planUpdateTime"), "yyyy-MM-dd HH:mm:ss");
		Byte status = Byte.parseByte(request.getParameter("status"));
		int  forceUpdate = Integer.parseInt(request.getParameter("forceUpdate"));
		String apkRelativePath = request.getParameter("apkRelativePath");
		String content = request.getParameter("content");
		int lastUpdateUserId = Integer.parseInt(request.getParameter("lastUpdateUserId"));
		
		logger.info("<<<<<<<<<<<<<< modify app version, appVersionId=" + appVersionId + ", versionName=" + versionName + 
				", versionCode=" + versionCode + ", planUpdateTime=" + planUpdateTime + ", status=" + status + ", forceUpdate=" + forceUpdate + 
				", apkRelativePath=" + apkRelativePath + ", lastUpdateUserId=" + lastUpdateUserId  + ", content=" + content);
		
		AppVersion appVersion = new AppVersion();
		appVersion.setAppVersionId(appVersionId);
		appVersion.setVersionName(versionName);
		appVersion.setVersionCode(versionCode);
		appVersion.setPlanUpdateTime(planUpdateTime);
		appVersion.setStatus(status);
		appVersion.setForceUpdate(forceUpdate);
		appVersion.setApkRelativePath(apkRelativePath);
		appVersion.setContent(content);
		appVersion.setLastUpdateTime(Integer.parseInt(System.currentTimeMillis()/1000 + ""));
		appVersion.setLastUpdateUserId(lastUpdateUserId);
		
		appData.modifyAppVersion(appVersion);
		return new ApiResult();
	}
	
	/*
	 * 删除appVersion
	 */
	@RequestMapping("/deleteAppVersion")
	public ApiResult deleteAppVersion(HttpServletRequest request) {
		int appVersionId = Integer.parseInt(request.getParameter("appVersionId"));
		
		logger.info("<<<<<<<<<<<<<< delete app version, appVersionId=" + appVersionId);
				
		appData.deleteAppVersion(appVersionId);
		return new ApiResult();
	}
	
	/*
	 * 上传Apk
	 */
	@RequestMapping("/uploadApk")
	public ApiResult uploadApk(HttpServletRequest request) {
		String fileName = request.getParameter("fileName");
		
		logger.info("<<<<<<<<<<<<<< upload Apk, fileName=" + fileName);
				
		Map<String, String> map = appData.uploadApk(fileName);
		return new ApiResult(map);
	}
	
	/*
	 * 查询 系统消息
	 */
	@RequestMapping("/getPusthMsg")
	public ApiResult getPusthMsg(HttpServletRequest request) {
		int pageNo = Integer.parseInt(request.getParameter("pageNo"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		
		logger.info("<<<<<<<<<<<<<< get Push Msg, pageNo=" + pageNo + ", pageSize=" + pageSize);
		
		PageInfo<PushMsg> page = appData.getPushMsgByPageList(pageNo, pageSize);
		return new ApiResult(page);
	}
	
	/*
	 * 添加 系统消息
	 */
	@RequestMapping("/savePushMsg")
	public ApiResult savePushMsg(HttpServletRequest request) {
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String picUrl = request.getParameter("picUrl");
		int type = 0;
		
		logger.info("<<<<<<<<<<<<<< save Push Msg, title=" + title + ", picUrl=" + picUrl + ", content=" + content);
		
		PushMsg pushMsg = new PushMsg();
		pushMsg.setTitle(title);
		pushMsg.setContent(content);
		pushMsg.setPicUrl(picUrl);
		pushMsg.setType(type);
		pushMsg.setCreateTime(Integer.parseInt(System.currentTimeMillis()/1000 + ""));
				
		appData.savePushmsg(pushMsg);
		return new ApiResult();
	}
	
	/*
	 * 查询 吧一吧详情
	 */
	@RequestMapping("/queryAppDetail")
	public ApiResult queryAppDetail(HttpServletRequest request) {
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String snbid = request.getParameter("snbid");
		String barName = request.getParameter("barName");
		int pageNo = Integer.parseInt(request.getParameter("pageNo"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		
		logger.info("<<<<<<<<<<<<<< queryAppDetail, province=" + province + ", city=" + city + ", snbid=" + snbid + ", barName=" + barName + ", pageNo=" + pageNo + ", pageSize=" + pageSize);
		
		PageInfo<AppBarDetail> page = appData.queryAppDetail(province, city, snbid, barName, pageNo, pageSize);
				
		return new ApiResult(page);
	}
	
	/*
	 * 获取吧一吧下面的二维码
	 */
	@RequestMapping("/qrCode")
	public void qrCode(HttpServletRequest request, HttpServletResponse response){
		int barId = Integer.parseInt(request.getParameter("internetBarId"));
		int barName = Integer.parseInt(request.getParameter("barName"));
		
		logger.info("<<<<<<<<<<<<<< qrCode, barId=" + barId + ", barName=" + barName);
		try{
			String url ="http://www.96ni.net/app/index.htm?f=";
			String barInfo ="{\"barId\":"+request.getParameter("barId")+",\"barName\":\""+request.getParameter("barName")+"\"}";
			barInfo = DESUtils.encryptDES(barInfo, "salt#&@!".getBytes(), "wy$@tere");
			QREncoder.generateToStream(url + barInfo, response.getOutputStream(), "jpeg", 512);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/*
	 * 查询 开通APP设置
	 */
	@RequestMapping("/queryAppSetting")
	public ApiResult queryAppSetting(HttpServletRequest request) {
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String snbid = request.getParameter("snbid");
		String barName = request.getParameter("barName");
		int pageNo = Integer.parseInt(request.getParameter("pageNo"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		
		logger.info("<<<<<<<<<<<<<< queryAppSetting, province=" + province + ", city=" + city + ", snbid=" + snbid + ", barName=" + barName + ", pageNo=" + pageNo + ", pageSize=" + pageSize);
		
		PageInfo<InternetBar> page = appData.queryAppSetting(province, city, snbid, barName, pageNo, pageSize);
				
		return new ApiResult(page);
	}
	
	/*
	 * 修改 开通APP设置
	 */
	@RequestMapping("/modifyAppSetting")
	public ApiResult modifyAppSetting(HttpServletRequest request) {
		String snbids = request.getParameter("snbids") ;
		int getIntoLb = Integer.parseInt(request.getParameter("getIntoLb"));
		int switch1 = Integer.parseInt(request.getParameter("switch1"));
		int switch2 = Integer.parseInt(request.getParameter("switch2"));
		int openAppOrder = Integer.parseInt(request.getParameter("openAppOrder"));
		int rechargeSwitch = Integer.parseInt(request.getParameter("rechargeSwitch"));
		int rechargeTotalSwitch = Integer.parseInt(request.getParameter("rechargeTotalSwitch"));
		
		logger.info("<<<<<<<<<<<<<< modifyAppSetting, snbids=" + snbids + ", getIntoLb=" + getIntoLb + ", switch1=" + switch1 + ", switch2=" + switch2 
				+ ", openAppOrder=" + openAppOrder + ", rechargeSwitch=" + rechargeSwitch + ", rechargeTotalSwitch=" + rechargeTotalSwitch);
		
		String exceptionInfo = appData.modifyAppSetting(snbids, getIntoLb, switch1, switch2, openAppOrder, rechargeSwitch, rechargeTotalSwitch);
				
		return new ApiResult(exceptionInfo);
	}
	
	/*
	 * 查询 充值报名(审核)
	 */
	@RequestMapping("/queryExamine")
	public ApiResult queryExamine(HttpServletRequest request) {
		int status = Integer.parseInt(request.getParameter("status"));
		int pageNo = Integer.parseInt(request.getParameter("pageNo"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		
		logger.info("<<<<<<<<<<<<<< queryExamine, status=" + status + ", pageNo=" + pageNo + ", pageSize=" + pageSize);
		
		PageInfo<BarInfo> page = appData.queryExamine(status, pageNo, pageSize);
				
		return new ApiResult(page);
	}
}
